package com.celesea.workflow.demo.v1.controller;

import com.celesea.framework.util.Page;
import com.celesea.framework.util.PageData;
import com.celesea.framework.util.Sort;
import com.celesea.workflow.demo.service.DemoService;
import com.celesea.workflow.demo.v1.controller.request.PostRequest;
import com.celesea.workflow.demo.v1.controller.request.PutRequest;
import com.celesea.workflow.demo.v1.controller.resource.DemoResource;
import com.celesea.workflow.demo.vo.DemoFilter;
import com.celesea.workflow.demo.vo.DemoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author JackYuan
 * @create 2017-11-18 15:42
 */

@RestController
@RequestMapping("v1/demos")
@Api(tags = "测试例子")
public class DemoController {

    @Autowired
    private DemoService demoService;


    @GetMapping
    @ApiOperation(value = "分页查询")
    public PageData<DemoResource> index(Page page, Sort sort) {
        DemoFilter filter = new DemoFilter();
        PageData<DemoVo> pageData = demoService.selectPage(filter, page, sort);
        return pageData.convert(item -> new DemoResource(item));
    }


    @GetMapping("{id}")
    @ApiOperation(value = "查询Demo")
    public DemoResource byId(@PathVariable String id) {
        DemoVo vo = demoService.selectById(id);
        return new DemoResource(vo);
    }

    @PostMapping
    @ApiOperation(value = "保存Demo")
    public DemoResource post(@RequestBody @Valid PostRequest request) {
        DemoVo vo = demoService.insert(new DemoVo(request));
        return new DemoResource(vo);
    }

    @PutMapping
    @ApiOperation(value = "保存Demo")
    public DemoResource put(@RequestBody @Valid PutRequest request) {
        DemoVo vo = demoService.update(new DemoVo(request));
        return new DemoResource(vo);
    }

}


